package com.gu.controller;

import com.github.pagehelper.PageInfo;
import com.gu.VO.LayuiVO;
import com.gu.VO.ServerResponse;
import com.gu.VO.UpdatePassword;
import com.gu.VO.UserVO;
import com.gu.entity.ApartmentDO;
import com.gu.entity.UserDO;
import com.gu.enumeration.UserStatusEnum;
import com.gu.service.ApartmentInfoService;
import com.gu.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.controller
 * @ClassName: UserController
 * @Author: gusongliao
 * @Description: 系统用户控制器
 * @createDate 2021/01/27 15:57
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    /**
    * @Description //查询用户
    * @User 谷松辽
    * @Date 2021/3/7 14:54
    * @param pageNum
    * @param pageSize
    * @param selectUserParam
    * @return com.gu.VO.LayuiVO
    */
    @RequestMapping("/userList")
    @ResponseBody
    public LayuiVO userList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String selectUserParam){
        PageInfo userList = userService.userList(pageNum,pageSize,selectUserParam);
        return LayuiVO.success(userList.getTotal(),userList.getList());
    }

    /**
    * @Description //更新用户状态
    * @User 谷松辽
    * @Date 2021/3/7 15:08
    * @param userDO
    * @return com.gu.VO.ServerResponse
    */
    @PostMapping("/updateUserStatus")
    @ResponseBody
    public ServerResponse updateUserStatus(@RequestBody UserDO userDO){
        return userService.updateUserStatus(userDO);
    }

    @PostMapping("/deleteUser")
    @ResponseBody
    public ServerResponse deleteUser(@RequestBody Integer id){
        return userService.deleteUserById(id);
    }

    @PostMapping("/addUser")
    @ResponseBody
    public ServerResponse addUser(@RequestBody UserDO userDO){
        return userService.insert(userDO);
    }

    @RequestMapping("/loginView")
    public String loginView(){
       return "login";
    }

    @RequestMapping("/forget")
    public String forget(){
        return "forget";
    }

    @RequestMapping("/resetPassword")
    @ResponseBody
    public ServerResponse resetPassword(@RequestBody UserDO userDO){
        return userService.selectUserByIdCard(userDO.getIdCard(),userDO.getUserName());
    }

    @RequestMapping("/firstLogin")
    public String isFirstLogin(){
        Boolean boo = apartmentInfoService.isFirstLogin();
        if (boo){
            return "firstLogin";
        }
        return "registerUser";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @PostMapping("/login")
    @ResponseBody
    public ServerResponse selectUserByUserName(String userPhone, String password, HttpServletRequest request, HttpServletResponse response){
        return userService.selectUserByUserPhone(userPhone,password,request,response);
    }

    @RequestMapping("/owner")
    public String owner(){
        return "indexUser";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        request.getServletContext().removeAttribute("user");
        request.getServletContext().removeAttribute("token");
        Cookie cookie = new Cookie("logout", null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "login";
    }

    @RequestMapping("/updatePassword")
    @ResponseBody
    public ServerResponse updatePassword(@RequestBody UpdatePassword updatePassword){
        return userService.updateUserPassword(updatePassword);
    }

    @RequestMapping("/forgetPassword")
    @ResponseBody
    public ServerResponse forgetPassword(@RequestBody UpdatePassword updatePassword){
        return userService.forgetUserPassword(updatePassword);
    }

    @PostMapping("/updateUserBanReason")
    @ResponseBody
    public ServerResponse updateUserBanReason(@RequestBody UserDO userDO){
        return userService.updateUserBanReason(userDO);
    }

    @RequestMapping("/updateAllUserInfo")
    @ResponseBody
    public ServerResponse updateAllUserInfo(@RequestBody UserDO userDO){
        return userService.updateAllUserInfo(userDO);
    }



}
